<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<!-- Casting SPELs in Lisp - Emacs Lisp Edition, a Comic Book
     Written by Conrad Barski, M.D., http://lisperati.com
     Edited by James A. Webb, http://uberkode.com -->

<head>

  <title>Casting SPELs in Lisp (15)</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="author" content="James A. Webb" />
  <meta name="date" content="2007-09-15T00:00:00+00:00" />
  <meta name="keywords" content="Emacs, Lisp" />

</head>

<body>

<center>

<table cellspacing="10" width="500">
  <tr>
    <td bgcolor="#AAFFAA" height="50" align="center">
        <b>The Functional Programming Style</b>
    </td>
  </tr>
  <tr>
    <td>
      You may have noticed that our <tt>describe-location</tt> 
      function seems pretty awkward in several different ways. First 
      of all, why are we passing in the variables for location and 
      map as parameters, instead of just reading our global variables 
      directly? The reason is that Lispers often like to write code 
      in the <i>Functional Programming</i> style. (To be clear, this 
      is completely unrelated in any way to the concepts called 
      "procedural programming" or "structural programming" that you 
      might have learned about in high school...) In this style, the 
      goal is to write functions that always follow the following 
      rules:
    </td>
  </tr>
  <tr>
    <td>
      <ol>
        <li>You only read variables that are passed into the function 
            or are created by the function (So you don't read any 
            global variables).</li>
        <li>You never change the value of a variable that has already 
            been set (So no incrementing variables or other such 
            foolishness).</li>
        <li>You never interact with the outside world, besides 
            returning a result value. (So no writing to files, no 
            writing messages for the user).</li>
      </ol>
    </td>
  </tr>
  <tr>
    <td>
      You may be wondering if you can actually write any code like 
      this that actually does anything useful, given these brutal 
      restrictions... The answer is yes, once you get used to the 
      style! Why would anyone bother following these rules? One 
      very important reason: Writing code in this style gives your 
      program <i>referential transparency</i>: This means that a 
      given piece of code, called with the same parameters, always 
      positively returns the same result and does exactly the same 
      thing no matter when you call it. This can reduce programming 
      errors and is believed to improve programmer productivity in 
      many cases.
    </td>
  </tr>
  <tr>
    <td>
      Of course, you'll always have some functions that are not 
      <i>functional</i> in style or you couldn't communicate with 
      the user or other parts of the outside world. Most of the 
      functions later in this tutorial do <u>not</u> follow these 
      rules.
    </td>
  </tr>
  <tr>
    <td align="center">
      &lt;&lt; <a href="casting-spels-emacs-1.html">begin</a>
      &lt; <a href="casting-spels-emacs-14.html">previous</a> - 
      <a href="casting-spels-emacs-16.html">next</a> &gt;
      <a href="casting-spels-emacs-35.html" >end</a> &gt;&gt;
    </td>
  </tr>
</table>

</center>

</body>

</html>